/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladores;

import controladores.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Deuda;
import modelo.Detallenomina;
import modelo.Libranza;

/**
 *
 * @author Ususario
 */
public class LibranzaJpaController implements Serializable {

    public LibranzaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Libranza libranza) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Deuda deudaIddeuda = libranza.getDeudaIddeuda();
            if (deudaIddeuda != null) {
                deudaIddeuda = em.getReference(deudaIddeuda.getClass(), deudaIddeuda.getIddeuda());
                libranza.setDeudaIddeuda(deudaIddeuda);
            }
            Detallenomina detallenominaIddetallenomina = libranza.getDetallenominaIddetallenomina();
            if (detallenominaIddetallenomina != null) {
                detallenominaIddetallenomina = em.getReference(detallenominaIddetallenomina.getClass(), detallenominaIddetallenomina.getIddetallenomina());
                libranza.setDetallenominaIddetallenomina(detallenominaIddetallenomina);
            }
            em.persist(libranza);
            if (deudaIddeuda != null) {
                deudaIddeuda.getLibranzaList().add(libranza);
                deudaIddeuda = em.merge(deudaIddeuda);
            }
            if (detallenominaIddetallenomina != null) {
                detallenominaIddetallenomina.getLibranzaList().add(libranza);
                detallenominaIddetallenomina = em.merge(detallenominaIddetallenomina);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Libranza libranza) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Libranza persistentLibranza = em.find(Libranza.class, libranza.getIdlibranza());
            Deuda deudaIddeudaOld = persistentLibranza.getDeudaIddeuda();
            Deuda deudaIddeudaNew = libranza.getDeudaIddeuda();
            Detallenomina detallenominaIddetallenominaOld = persistentLibranza.getDetallenominaIddetallenomina();
            Detallenomina detallenominaIddetallenominaNew = libranza.getDetallenominaIddetallenomina();
            if (deudaIddeudaNew != null) {
                deudaIddeudaNew = em.getReference(deudaIddeudaNew.getClass(), deudaIddeudaNew.getIddeuda());
                libranza.setDeudaIddeuda(deudaIddeudaNew);
            }
            if (detallenominaIddetallenominaNew != null) {
                detallenominaIddetallenominaNew = em.getReference(detallenominaIddetallenominaNew.getClass(), detallenominaIddetallenominaNew.getIddetallenomina());
                libranza.setDetallenominaIddetallenomina(detallenominaIddetallenominaNew);
            }
            libranza = em.merge(libranza);
            if (deudaIddeudaOld != null && !deudaIddeudaOld.equals(deudaIddeudaNew)) {
                deudaIddeudaOld.getLibranzaList().remove(libranza);
                deudaIddeudaOld = em.merge(deudaIddeudaOld);
            }
            if (deudaIddeudaNew != null && !deudaIddeudaNew.equals(deudaIddeudaOld)) {
                deudaIddeudaNew.getLibranzaList().add(libranza);
                deudaIddeudaNew = em.merge(deudaIddeudaNew);
            }
            if (detallenominaIddetallenominaOld != null && !detallenominaIddetallenominaOld.equals(detallenominaIddetallenominaNew)) {
                detallenominaIddetallenominaOld.getLibranzaList().remove(libranza);
                detallenominaIddetallenominaOld = em.merge(detallenominaIddetallenominaOld);
            }
            if (detallenominaIddetallenominaNew != null && !detallenominaIddetallenominaNew.equals(detallenominaIddetallenominaOld)) {
                detallenominaIddetallenominaNew.getLibranzaList().add(libranza);
                detallenominaIddetallenominaNew = em.merge(detallenominaIddetallenominaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = libranza.getIdlibranza();
                if (findLibranza(id) == null) {
                    throw new NonexistentEntityException("The libranza with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Libranza libranza;
            try {
                libranza = em.getReference(Libranza.class, id);
                libranza.getIdlibranza();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The libranza with id " + id + " no longer exists.", enfe);
            }
            Deuda deudaIddeuda = libranza.getDeudaIddeuda();
            if (deudaIddeuda != null) {
                deudaIddeuda.getLibranzaList().remove(libranza);
                deudaIddeuda = em.merge(deudaIddeuda);
            }
            Detallenomina detallenominaIddetallenomina = libranza.getDetallenominaIddetallenomina();
            if (detallenominaIddetallenomina != null) {
                detallenominaIddetallenomina.getLibranzaList().remove(libranza);
                detallenominaIddetallenomina = em.merge(detallenominaIddetallenomina);
            }
            em.remove(libranza);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Libranza> findLibranzaEntities() {
        return findLibranzaEntities(true, -1, -1);
    }

    public List<Libranza> findLibranzaEntities(int maxResults, int firstResult) {
        return findLibranzaEntities(false, maxResults, firstResult);
    }

    private List<Libranza> findLibranzaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Libranza.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Libranza findLibranza(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Libranza.class, id);
        } finally {
            em.close();
        }
    }

    public int getLibranzaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Libranza> rt = cq.from(Libranza.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
